package com.hz.web.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hz.web.service.IpageCountService;
import com.hz.web.util.Uid;

/**
 * 统计网站浏览量
 * @author Administrator
 *
 */
@Controller
@RequestMapping("pageCount")
public class PageCountController {

	@Resource
	private IpageCountService pageCountService;
	
	/**
	 * 查询次数
	 * @return
	 */
	@RequestMapping("queryById")
	@ResponseBody
	public Object queryById(){
		int count = pageCountService.queryById();
		return count;
				
	}
	/**
	 * 自增长
	 * @return
	 */
	@RequestMapping("updateAdd")
	@ResponseBody
	public Object updateAdd(HttpServletRequest request){
		int count = pageCountService.updateAdd();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		
		String ip = getIpAddr(request);
		Calendar a=Calendar.getInstance();
		int year = a.get(Calendar.YEAR);
		int month = a.get(Calendar.MONTH)+1;
		parmsMap.put("ipLogId", Uid.getUid());
		parmsMap.put("ipAddress", ip);
		parmsMap.put("logYear", year);
		parmsMap.put("logMonth", month);
		return count;
	}
	
	
	 private String getIpAddr(HttpServletRequest request) {      
         String ip = request.getHeader("x-forwarded-for");      
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {      
            ip = request.getHeader("Proxy-Client-IP");      
        }      
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {      
            ip = request.getHeader("WL-Proxy-Client-IP");      
         }      
       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {      
             ip = request.getRemoteAddr();      
        }      
       return ip;      
  }
}
